<?php

namespace app\service;

use app\admin\model\agency\AgencyInfo;
use app\admin\model\play\Order;
use app\common\model\EarningsLog;
use app\common\model\MoneyLog;
use app\common\model\User;
use think\Config;
use think\Exception;

class EarningsService
{

    public function run($oid,$type='play'){

        if($type=='play'){
            $order=Order::get(['id'=>$oid,'status'=>4]);
            if(empty($order)) throw new Exception('订单错误');
            //陪玩师收入
            MoneyLog::add($order->player_id,$order->pay_price,3,'订单收入',$order->order_no);
            //推广收益
            $user=User::get($order->uid);
            if($user->referee>0){
                $money = bcmul($order->pay_price,Config::get('site.member_earnings_rate') / 100 ,2);
                EarningsLog::add($user->referee,$money,5,'推广收益',$order->order_no);
            }
            //代理收益
            $agency=AgencyInfo::get(['city'=>$order->city]);
            if(!empty($agency)){
                $money = bcmul($order->pay_price,Config::get('site.agency_rate') / 100,2);
                EarningsLog::add($agency->uid,$money,4,'城市代理收益',$order->order_no);
            }
        }
    }
}